package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.UserPromotionSettleInfoMapper;
import com.tulele.vtool.model.UserPromotionSettleInfo;
import com.tulele.vtool.model.combinedEntity.CombinedIncomeRankingList;
import com.tulele.vtool.service.IUserPromotionSettleInfoService;
import com.tulele.vtool.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class UserPromotionSettleInfoServiceImpl implements IUserPromotionSettleInfoService {

    @Autowired
    private UserPromotionSettleInfoMapper userPromotionSettleInfoMapper;

    @Override
    public int processUserPromotionSettleInfo(UserPromotionSettleInfo userPromotionSettleInfo, double userChargeAmount, int settleLevel) {

        log.info("[userChargeAmount] = " + userChargeAmount + " [settleLevel] = " + settleLevel);

        double cumulativeIncome = Optional.ofNullable(userPromotionSettleInfo.getCumulativeIncome()).orElse(0d);

        if (settleLevel == 1) {

            int firstLevelProportion = Optional.ofNullable(userPromotionSettleInfo.getFirstLevelProportion()).orElse(0);

            if (firstLevelProportion > 0) {

                double firstLevelIncome = (userChargeAmount * firstLevelProportion) / 100;

                double firstLevelCumulativeIncome = Optional.ofNullable(userPromotionSettleInfo.getFirstLevelCumulativeIncome()).orElse(0d);

                UserPromotionSettleInfo finalUserPromotionSettleInfo = new UserPromotionSettleInfo();
                finalUserPromotionSettleInfo.setId(userPromotionSettleInfo.getId());
                finalUserPromotionSettleInfo.setFirstLevelCumulativeIncome(firstLevelCumulativeIncome + firstLevelIncome);
                finalUserPromotionSettleInfo.setCumulativeIncome(cumulativeIncome + firstLevelIncome);
                finalUserPromotionSettleInfo.setModifyTime(DateUtil.getCurDate());

                userPromotionSettleInfoMapper.updPromotionSettleInfo(finalUserPromotionSettleInfo);

            }

        } else if (settleLevel == 2) {

            int secondLevelProportion = Optional.ofNullable(userPromotionSettleInfo.getSecondLevelProportion()).orElse(0);

            if (secondLevelProportion > 0) {

                double secondLevelIncome = (userChargeAmount * secondLevelProportion) / 100;

                double secondLevelCumulativeIncome = Optional.ofNullable(userPromotionSettleInfo.getSecondLevelCumulativeIncome()).orElse(0d);

                UserPromotionSettleInfo finalUserPromotionSettleInfo = new UserPromotionSettleInfo();
                finalUserPromotionSettleInfo.setId(userPromotionSettleInfo.getId());
                finalUserPromotionSettleInfo.setSecondLevelCumulativeIncome(secondLevelCumulativeIncome + secondLevelIncome);
                finalUserPromotionSettleInfo.setCumulativeIncome(cumulativeIncome + secondLevelIncome);
                finalUserPromotionSettleInfo.setModifyTime(DateUtil.getCurDate());

                userPromotionSettleInfoMapper.updPromotionSettleInfo(finalUserPromotionSettleInfo);

            }

        }
        //20200226 添加
        else if (settleLevel == 3) {

            int thirdLevelProportion = Optional.ofNullable(userPromotionSettleInfo.getThirdLevelProportion()).orElse(0);

            if (thirdLevelProportion > 0) {

                double thirdLevelIncome = (userChargeAmount * thirdLevelProportion) / 100;

                double thirdLevelCumulativeIncome = Optional.ofNullable(userPromotionSettleInfo.getThirdLevelCumulativeIncome()).orElse(0d);

                UserPromotionSettleInfo finalUserPromotionSettleInfo = new UserPromotionSettleInfo();
                finalUserPromotionSettleInfo.setId(userPromotionSettleInfo.getId());
                finalUserPromotionSettleInfo.setThirdLevelCumulativeIncome(thirdLevelCumulativeIncome + thirdLevelIncome);
                finalUserPromotionSettleInfo.setCumulativeIncome(cumulativeIncome + thirdLevelIncome);
                finalUserPromotionSettleInfo.setModifyTime(DateUtil.getCurDate());

                userPromotionSettleInfoMapper.updPromotionSettleInfo(finalUserPromotionSettleInfo);

            }

        }
        //20200518 yl 添加
        else if (settleLevel == 4) {

            int fourthLevelProportion = Optional.ofNullable(userPromotionSettleInfo.getFourthLevelProportion()).orElse(0);

            if (fourthLevelProportion > 0) {

                double fourthLevelIncome = (userChargeAmount * fourthLevelProportion) / 100;

                double fourthLevelCumulativeIncome = Optional.ofNullable(userPromotionSettleInfo.getFourthLevelCumulativeIncome()).orElse(0d);

                UserPromotionSettleInfo finalUserPromotionSettleInfo = new UserPromotionSettleInfo();
                finalUserPromotionSettleInfo.setId(userPromotionSettleInfo.getId());
                finalUserPromotionSettleInfo.setFourthLevelCumulativeIncome(fourthLevelCumulativeIncome + fourthLevelIncome);
                finalUserPromotionSettleInfo.setCumulativeIncome(cumulativeIncome + fourthLevelIncome);
                finalUserPromotionSettleInfo.setModifyTime(DateUtil.getCurDate());

                userPromotionSettleInfoMapper.updPromotionSettleInfo(finalUserPromotionSettleInfo);

            }

        }

        //20200518 yl 添加
        else if (settleLevel == 5) {

            int fifthLevelProportion = Optional.ofNullable(userPromotionSettleInfo.getFifthLevelProportion()).orElse(0);

            if (fifthLevelProportion > 0) {

                double fifthLevelIncome = (userChargeAmount * fifthLevelProportion) / 100;

                double fifthLevelCumulativeIncome = Optional.ofNullable(userPromotionSettleInfo.getFifthLevelCumulativeIncome()).orElse(0d);

                UserPromotionSettleInfo finalUserPromotionSettleInfo = new UserPromotionSettleInfo();
                finalUserPromotionSettleInfo.setId(userPromotionSettleInfo.getId());
                finalUserPromotionSettleInfo.setFifthLevelCumulativeIncome(fifthLevelCumulativeIncome + fifthLevelIncome);
                finalUserPromotionSettleInfo.setCumulativeIncome(cumulativeIncome + fifthLevelIncome);
                finalUserPromotionSettleInfo.setModifyTime(DateUtil.getCurDate());

                userPromotionSettleInfoMapper.updPromotionSettleInfo(finalUserPromotionSettleInfo);

            }

        }

        // ljt 20201029 佣金抵扣统计 start
        else if (settleLevel == 6) {

            double allBrokerage = Optional.ofNullable(userPromotionSettleInfo.getAllBrokerage()).orElse(0d);

            // 佣金抵扣统计,减去统计数据的佣金总数
            double newCumulativeIncome = cumulativeIncome - userChargeAmount;
            if (newCumulativeIncome < 0) {
                newCumulativeIncome = 0;
            }

            UserPromotionSettleInfo setInfoByAllBrokerage = new UserPromotionSettleInfo();
            setInfoByAllBrokerage.setId(userPromotionSettleInfo.getId());
            setInfoByAllBrokerage.setAllBrokerage(allBrokerage + userChargeAmount);
            setInfoByAllBrokerage.setCumulativeIncome(newCumulativeIncome);
            setInfoByAllBrokerage.setModifyTime(DateUtil.getCurDate());

            userPromotionSettleInfoMapper.updPromotionSettleInfo(setInfoByAllBrokerage);

        }
        // ljt 20201029 佣金抵扣统计 end

        return 0;
    }

    @Override
    public UserPromotionSettleInfo getUserPromotionSettleInfo(Integer userId) {
        return userPromotionSettleInfoMapper.queryUserPromotionSettleInfo(userId);
    }

    @Override
    public int addPromotionSettleInfo(UserPromotionSettleInfo userPromotionSettleInfo) {
        return userPromotionSettleInfoMapper.addPromotionSettleInfo(userPromotionSettleInfo);
    }

    @Override
    public List<CombinedIncomeRankingList> getIncomeRankingList(String gzhCode) {
        return userPromotionSettleInfoMapper.getIncomeRankingList(gzhCode);
    }
}
